// 判断是否登录
import {TOKEN} from "./request";
import {queryUserInfo, userLogin} from "../api/user/user";
import {getAllWannaPerformanceIds} from "../api/user/concert";
import store from "../store";

export const isLogin = () => {
    let _token = uni.getStorageSync(TOKEN)
    return undefined !== _token && _token.length !== 0;
}

export function login() {
    wx.showModal({
        title: "温馨提示",
        content: "亲，授权微信登录后才能正常使用小程序功能",
        async success(res) {
            //如果用户点击了确定按钮
            if (res.confirm) {
                await wx.getUserProfile({
                    desc: "获取你的昵称、头像、地区及性别",
                    success: async (data) => {
                        await doLogin();
                    },
                    fail: (res) => {
                        //拒绝授权
                        wx.showToast({
                            title: "您拒绝了请求,不能正常使用小程序",
                            icon: "error",
                            duration: 2000,
                        });

                    },
                });
            } else if (res.cancel) {
                //如果用户点击了取消按钮
                wx.showToast({
                    title: "您拒绝了请求,不能正常使用小程序",
                    icon: "error",
                    duration: 2000,
                });

            }
        },
    });
}

export function doLogin() {
    return new Promise((resolve, reject) => {
        uni.showLoading({
            title: '登陆中...',
            duration: 10000,
        })
        // 调用微信登录接口
        wx.login({
            success: (res) => {
                if (res.code) {
                    userLogin({code: res.code})
                        .then((res) => {
                            uni.setStorage({
                                key: TOKEN,
                                data: res.data.token,
                                success: function () {
                                    uni.hideLoading();
                                    wx.showToast({
                                        title: "登陆成功！",
                                        icon: "success",
                                        duration: 2000,
                                    });
                                    // 获取想看
                                    getAllWannaPerformanceIds().then(res => {
                                        if (res.success) {
                                            store.commit("initUserWanna", res.data);
                                        }
                                    })
                                    // 查询用户详情
                                    queryUserInfo().then((res) => {
                                        if (res.success) {
                                            store.commit("saveUserInfo", res.data);
                                        }
                                    });
                                    resolve();
                                },
                                fail: function (err) {
                                    uni.hideLoading();
                                    wx.showToast({
                                        title: "登陆失败！",
                                        icon: "error",
                                        duration: 2000,
                                    });
                                    reject(err);
                                },
                            });
                        })
                        .catch((err) => {
                            uni.hideLoading();
                            wx.showToast({
                                title: "登陆失败！",
                                icon: "error",
                                duration: 2000,
                            });
                            console.error("用户登录失败：", err);
                            reject(err);
                        });
                } else {
                    uni.hideLoading();
                    wx.showToast({
                        title: "登陆失败！",
                        icon: "error",
                        duration: 2000,
                    });
                    console.log("微信登录失败：", res.errMsg);
                    reject(res.errMsg);
                }
            },
            fail: (err) => {
                uni.hideLoading();
                wx.showToast({
                    title: "登陆失败！",
                    icon: "error",
                    duration: 2000,
                });
                console.error("调用微信登录接口失败：", err);
                reject(err);
            },
        });
    });
}